package com.lm.mail.example;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

public class MailExample {


    public static void sendMailWithAttachment() {
        String host = "smtp.gmail.com";
        String from = "your_email@gmail.com";
        String password = "your_password";
        String to = "recipient_email@gmail.com";

        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, password);
            }
        });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject("带附件的邮件测试");

            // 创建邮件正文
            MimeBodyPart textPart = new MimeBodyPart();
            textPart.setText("请查看附件！");

            // 创建附件部分
            MimeBodyPart attachmentPart = new MimeBodyPart();
            attachmentPart.attachFile("path/to/file.txt"); // 附件路径

            // 合并正文和附件
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(textPart);
            multipart.addBodyPart(attachmentPart);

            message.setContent(multipart);

            Transport.send(message);
            System.out.println("带附件的邮件发送成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void sendHtmlMail() throws Exception{
        String host = "smtp.gmail.com";
        String from = "your_email@gmail.com";
        String password = "your_password";
        String to = "recipient_email@gmail.com";

        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, password);
            }
        });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject("HTML 邮件测试");

            // 设置 HTML 内容
            String htmlContent = "<h1>欢迎使用 Java Mail</h1><p>这是一个 <b>HTML</b> 格式的邮件！</p>";
            message.setContent(htmlContent, "text/html;charset=UTF-8");

            Transport.send(message);
            System.out.println("HTML 邮件发送成功！");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    public static void sendTextMail() throws Exception {
        String host = "smtp.126.com";  // SMTP 服务器地址 gmail : smtp.gmail.com , qq: smtp.qq.com, 126: smtp.126.com
        String from = "you@126.com";
        String password = "1";
        String to = "352356713@qq.com";

        // 配置属性
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.ssl.protocols", "TLSv1.2");

        // 获取会话对象
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, password);
            }
        });

        try {
            // 创建邮件
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject("Java Mail 测试");
            message.setText("这是一封通过 Java Mail 发送的简单文本邮件！");

            // 发送邮件
            Transport.send(message);
            System.out.println("邮件发送成功！");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        sendTextMail();
    }
}

